//
//  UIStandardSelectButton.m
//  vhStressTest1
//
//  Created by jia yu on 2021/8/31.
//

#import <vhECGTools/vhECGTools.h>
#import "UIStandardSelectButton.h"
#import "UIStandardSelectViewController.h"
#import <ZMFoundation/ZMFoundation.h>

@interface UIStandardSelectButton(){
    float screenRatio;
    UIImage *bgImage;
}
@end

@implementation UIStandardSelectButton
@synthesize availableItems;
@synthesize inner;
@synthesize changedHandler;
@synthesize style;
@synthesize canSelectNone;
@synthesize defaultSelectedString;

-(id)initWithFrame:(CGRect)frame{
    NSLog(@"Warning please UIStandardSelectButton -(id)initWithFrame:(CGRect)frame scale:(float)scale");
    return [[UIStandardSelectButton alloc] initWithFrame:frame scale:0.5f];
}

-(id)initWithFrame:(CGRect)frame scale:(float)scale{
    self = [super initWithFrame:frame];
    if (self){
        screenRatio = scale;
        canSelectNone = NO;
        style = UIStandardSelectButton_Style_Default;
        self.backgroundColor = [vhECGTools colorFromHEXString:@"f7fafc"];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 12.f * screenRatio;
        [self addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        self.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:34.f * screenRatio];
        [self setTitleColor:[vhECGTools colorFromHEXString:@"294559"] forState:UIControlStateNormal];
        [self setTitleColor:[vhECGTools colorFromHEXString:@"294559" withAlpha:0.4] forState:UIControlStateDisabled];

        NSBundle *frameBundle = [NSBundle ex_bundleWithName:@"vhECGTools"];
        NSBundle *myBundle = [[NSBundle alloc] initWithPath:[frameBundle pathForResource:@"standard" ofType:@"bundle"]];
        
        bgImage = [[UIImage alloc] initWithContentsOfFile:[myBundle pathForResource:@"select" ofType:@"png"]];
        UIImage *image = [vhECGTools scaleImage:bgImage to:CGSizeMake(frame.size.height, frame.size.height)];
        image = [image stretchableImageWithLeftCapWidth:1 topCapHeight:0];
        [self setBackgroundImage:image forState:UIControlStateNormal];
        
        [self setTitleEdgeInsets:UIEdgeInsetsMake(0.f, 18.f * screenRatio, 0.f, 20.f * screenRatio)];

        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeading;
    }
    return self;
}

-(BOOL)availableShowingtext{
    for (NSItemObject *item in availableItems){
        if ([item.str isEqualToString:self.titleLabel.text]){
            return YES;
        }
    }
    return NO;
}

-(void)buttonAction:(id)sender{
    UIViewController *currentViewController = [vhECGTools currentRootVC];
    if (nil == currentViewController){
        NSLog(@"UIStandardSelectButton type class Not Found rootViewController for presentViewController.");
    }
    UIStandardSelectViewController *v = [[UIStandardSelectViewController alloc] initWithScale:screenRatio];
    v.fromView = currentViewController.view;
    v.fromViewRect = [self.superview convertRect:self.frame toView:currentViewController.view];
    if (canSelectNone){
        NSMutableArray *items = [[NSMutableArray alloc] initWithArray:self.availableItems];
        NSItemObject *noneItem = [[NSItemObject alloc] init];
        noneItem.str = @" ";
        noneItem.inner = -1;
        [items insertObject:noneItem atIndex:0];
        v.availableItems = items;
    }else{
        v.availableItems = self.availableItems;
    }
    v.selectedString = self.titleLabel.text;
    if (defaultSelectedString != nil
        && defaultSelectedString.length > 0
        &&  NO == [self availableShowingtext]){
        v.selectedString = self.defaultSelectedString;
    }
    v.handler = ^(NSItemObject * _Nonnull selectedItem) {
        [self setTitle:selectedItem.str forState:UIControlStateNormal];
        self.inner = selectedItem.inner;
        if (self->changedHandler){
            self->changedHandler(selectedItem.str);
        }
    };
    [currentViewController presentViewController:v animated:NO completion:^{

    }];
    return;
}

-(void)setStyle:(UIStandardSelectButton_Style)_style{
    style = _style;
    switch (style) {
        case UIStandardSelectButton_Style_Board:
            self.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:34.f * screenRatio];
            self.layer.borderColor = [vhECGTools colorFromHEXString:@"dde0e2"].CGColor;
            self.layer.borderWidth = 2.3f * screenRatio;
            self.backgroundColor = [vhECGTools colorFromHEXString:@"f7fafc"];
            break;
        default:
            self.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:34.f * screenRatio];
            self.backgroundColor = [vhECGTools colorFromHEXString:@"f7fafc"];
            self.layer.borderWidth = 0.f;
            break;
    }
}
@end
